package com.ews.onlineexchange.controller.activitii;

import java.util.Arrays;
import java.util.List;

import javax.enterprise.inject.Model;

import com.ews.onlineexchange.model.User;
import org.activiti.engine.task.TaskQuery;
import org.apache.commons.lang3.StringUtils;

@Model
public class TodoTaskList extends BpmList {
	private   String				supplierName;
	private   String				name;
	private   String				dictId;
	private	  String				assignee;
	private   String				projectName;
	@Override
	TaskQuery createTaskQuery() {
		TaskQuery taskQuery = taskService.createTaskQuery();
		taskQuery.or();
		User loginedUser = userService.getLoginedUser();
		String username = loginedUser.getUsername();
//		taskQuery.taskAssignee( username );

		String roleStr = loginedUser.getRoles();
		String[] roleArray = roleStr == null ? new String[]{} : roleStr.split("、");
//		List<String> roles = keycloakUserService.queryRolesOfUser( username );
		List<String> roles = Arrays.asList(roleArray);
//		taskQuery.taskAssignee( username );
		taskQuery.taskCandidateGroupIn( roles );
		taskQuery.endOr();
		taskQuery.includeProcessVariables();
		if(StringUtils.isNoneBlank(supplierName)) {
			taskQuery.processVariableValueLike("supplierName", getStringRestriction(supplierName));
		}
		if(StringUtils.isNoneBlank(name)) {
			taskQuery.taskNameLike(getStringRestriction(name));
		}
		if(StringUtils.isNoneBlank(dictId)) {
			taskQuery.processVariableValueEquals("dictId", dictId);
		}
		if(StringUtils.isNoneBlank(projectName)) {
			taskQuery.processVariableValueEquals("projectName", projectName);
		}
		if(StringUtils.isNoneBlank(assignee)) {
			taskQuery.taskAssigneeLike(getStringRestriction(assignee));
		}
		User user = userService.getLoginedUser();
		if(user != null){
			String userRegion = user.getRegion();
			//针对取消报名待办任务，supplierRegion表示的是buyerRegion
			if("370501".equals(user.getRegion()))taskQuery.processVariableValueLessThanOrEqual("supplierRegion",user.getRegion());
			else taskQuery.processVariableValueEquals("supplierRegion",user.getRegion());
		}
		taskQuery.orderByTaskCreateTime().desc();
		return taskQuery;
	}
	
	public String getSupplierName() {
		return supplierName;
	}
	public void setSupplierName(String supplierName) {
		this.supplierName = supplierName;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDictId() {
		return dictId;
	}
	public void setDictId(String dictId) {
		this.dictId = dictId;
	}
	public String getAssignee() {
		return assignee;
	}
	public void setAssignee(String assignee) {
		this.assignee = assignee;
	}
	public String getProjectName() {
		return projectName;
	}
	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}
	
	
}
